//
// Created by Administrator on 2025/3/12.
//

#include "ui_word_query_with_keyboard.h"
//ui_word_query_main_screen_t ui_word_query_keyboard_screen = {0};
//word_query_module_t word_query_module = {.data=&ui_word_query_data,.screen=&ui_word_query_keyboard_screen};
static void fresh_recently_word_list(word_query_word_entry_t *word_list,int size,lv_obj_t *prev_obj,int pad_row);
static void ui_fresh_search_word_list();

static void ui_word_query_keyboard_left_key(lv_group_t *group, lv_obj_t *obj, word_query_event_data_t *data);

static void ui_word_query_right_keyboard_key(lv_group_t *group, lv_obj_t *obj, word_query_event_data_t *data);


static void word_query_keyboard_del_key(lv_group_t *group, lv_obj_t *obj, word_query_event_data_t *data);

static void word_query_keyboard_space_key(lv_group_t *group, lv_obj_t *obj, word_query_event_data_t *data);


static void ui_word_query_keyboard_left_key(lv_group_t *group, lv_obj_t *obj, word_query_event_data_t *data) {
    lv_group_focus_obj(data->prev_obj);
}

static void ui_word_query_right_keyboard_key(lv_group_t *group, lv_obj_t *obj, word_query_event_data_t *data) {
    lv_group_focus_obj(data->next_obj);
}

static void ui_word_query_enter_keyboard_key(lv_group_t *group, lv_obj_t *obj, word_query_event_data_t *data) {
    if (data->obj_type == UI_WORD_QUERY_OBJ_TYPE_TOP_PANEL_LABEL)
    {
        LOG_INFO_WRAPPER("UI_WORD_QUERY_OBJ_TYPE_TOP_PANEL_LABEL");
        word_query_module.screen->deinit_cb();
    }
}

// 输入字母
static void word_query_keyboard_space_key(lv_group_t *group, lv_obj_t *obj, word_query_event_data_t *data) {
    // a-z随机抽取一个
    // 生成随机小写字母 a-z
    char random_char = 'a' + (rand() % 26);  // 'a' (97) ~ 'z' (122)
    LOG_INFO_WRAPPER("random_char:%c",random_char);
    // 将letter_str追加
    if(word_query_module.data->input_word_len < INPUT_BOX_MAX_LEN){
        char temp[2] = {random_char, '\0'};
        strcat(word_query_module.data->input_word, temp);
        word_query_module.data->input_word_len++;
        LOG_INFO_WRAPPER("input_word:%s",word_query_module.data->input_word);
        ui_fresh_search_word_list();
        // focus 输入框
        lv_obj_t *input_panel = common_ui_comp_get_child(word_query_module.screen->screen,
                                                         UI_COMP_WORD_QUERY_KEYBOARD_INPUT_PANEL);
        lv_group_focus_obj(input_panel);
    }
}

// 删除字母
static void word_query_keyboard_del_key(lv_group_t *group, lv_obj_t *obj, word_query_event_data_t *data) {
    // 将word_query_module.data->input_word的长度减一
    if (word_query_module.data->input_word_len > 0){
        word_query_module.data->input_word[word_query_module.data->input_word_len-1] = '\0';
        word_query_module.data->input_word_len--;
        LOG_INFO_WRAPPER("input_word:%s",word_query_module.data->input_word);
        ui_fresh_search_word_list();
        // focus 输入框
        lv_obj_t *input_panel = common_ui_comp_get_child(word_query_module.screen->screen,
                                                         UI_COMP_WORD_QUERY_KEYBOARD_INPUT_PANEL);
        lv_group_focus_obj(input_panel);
    }
}






static void ui_word_query_keyboard_screen_event(lv_event_t *e){
    LOG_INFO_WRAPPER("start");
//    lv_group_send_data()
    lv_event_code_t event_code = lv_event_get_code(e);
    lv_obj_t *target = lv_event_get_target(e);
    lv_group_t *group = lv_group_get_default();
    word_query_event_data_t * event_data = (word_query_event_data_t *)lv_event_get_user_data(e);
    if (event_code == LV_EVENT_KEY)
    {
        // 按键事件 获取按键值
        uint32_t lv_key = lv_event_get_key(e);
        switch (lv_key)
        {
            case LV_KEY_UP:
                LV_LOG_USER("=========== touched KeyUp");
                ui_word_query_keyboard_left_key(group,target,event_data);
                break;
            case LV_KEY_DOWN:
                LV_LOG_USER("============ touched KeyDown");
                ui_word_query_right_keyboard_key(group, target, event_data);
                break;
            case LV_KEY_LEFT:
                LV_LOG_USER("=========== touched KeyLeft");
                ui_word_query_keyboard_left_key(group,target,event_data);
//                lv_group_focus_prev(group);

                break;
            case LV_KEY_RIGHT:
                LV_LOG_USER("============ touched KeyRight");
                ui_word_query_right_keyboard_key(group, target, event_data);
//                lv_group_focus_next(group);

                break;
            case LV_KEY_ENTER:
                LV_LOG_USER("============ touched KeyEnter");
//                ui_word_query_enter_key(group,target,event_data);
                ui_word_query_enter_keyboard_key(group, target, event_data);

                break;
            case LV_KEY_BACKSPACE:
                LV_LOG_USER("============ touched KeyBackspace");

                break;
            case LV_KEY_DEL:
                // 模拟删除字母
                LV_LOG_USER("============ touched keydel");
                word_query_keyboard_del_key(group, target, event_data);
                break;
            case LV_KEY_SPACE:
                // 模拟输入字母
                LV_LOG_USER("============ touched space");
                word_query_keyboard_space_key(group, target, event_data);
                break;
            case LV_KEY_ESC:
                LV_LOG_USER("============ touched ESC");
                word_query_keyboard_switch_dao();
                break;
            default:
                break;
        }
    }
}




// 添加事件
static void word_query_keyboard_add_event_common(lv_obj_t *obj,word_query_event_data_t * event_data){
    lv_group_add_obj(word_query_module.screen->group,obj);
    // 设置 user_data（存储 event_data）
    if(event_data){
        lv_obj_set_user_data(obj, event_data);
        lv_obj_add_event_cb(obj, common_del_event_data_event_cb, LV_EVENT_DELETE,
                            event_data);
    }
    // 删除事件时回调
    lv_obj_add_event_cb(obj, ui_word_query_keyboard_screen_event, LV_EVENT_KEY,
                        event_data);

}

// 界面初始化
void ui_word_query_keyboard_screen_init(ui_word_query_main_screen_t *screen){
    LOG_INFO_WRAPPER("start");
    if(screen->screen == NULL){
        lv_obj_t **children = lv_mem_alloc(sizeof(lv_obj_t *) * UI_COMP_WORD_QUERY_SCREEN_NUM);
        if(children == NULL){
            LV_LOG_ERROR("ui_WordSpellScreen_screen_init children malloc failed");
            return;
        }
        if(screen->group == NULL){
            screen->group = ui_set_indev_group(indev_keypad);
        }
        screen->screen = ui_create_default_screen(lv_scr_act());
        // 主体列对齐panel
        lv_obj_t *main_panel = ui_create_column_default_panel(screen->screen);


        // 顶部行对齐panel
        lv_obj_t *top_panel = ui_word_query_top_panel_component(main_panel);
        lv_obj_t *top_return_panel = lv_obj_get_child(top_panel, 1);
        lv_obj_set_style_border_side(top_return_panel, LV_BORDER_SIDE_BOTTOM,LV_STATE_FOCUSED);
        lv_obj_set_style_border_width(top_return_panel, 2, LV_STATE_FOCUSED);
        word_query_event_data_t *event_data = malloc_set_zero(sizeof(word_query_event_data_t));
        event_data->obj_type = UI_WORD_QUERY_OBJ_TYPE_TOP_PANEL_LABEL;
        word_query_keyboard_add_event_common(top_return_panel, event_data);
        lv_coord_t top_panel_height = lv_obj_get_style_height(top_panel, 0);

        // 中间行对齐panel all 宽 210 高
        lv_obj_t *middle_panel = ui_create_column_panel(main_panel, lv_disp_get_hor_res(NULL),210);
        lv_coord_t middle_panel_row_pad = 16;
        lv_obj_set_style_pad_row(middle_panel, middle_panel_row_pad, 0);

        // 中间顶部输入框
        // 文本距离界面左边的距离
        lv_coord_t label_left_margin = 26;

        lv_coord_t middle_top_panel_height = 40;
        lv_obj_t *middle_top_panel = ui_create_without_flex_panel(middle_panel, lv_disp_get_hor_res(NULL), middle_top_panel_height);
        lv_coord_t middle_top_input_panel_width = 399;
        lv_coord_t middle_top_input_panel_border_width = 1;

        // 输入框的padleft
        lv_obj_t *middle_top_input_panel = ui_create_without_flex_panel(middle_top_panel, middle_top_input_panel_width, 27);
        event_data = malloc_set_zero(sizeof(word_query_event_data_t));
        event_data->prev_obj = top_return_panel;
        word_query_keyboard_add_event_common(middle_top_input_panel, event_data);
        children[UI_COMP_WORD_QUERY_KEYBOARD_INPUT_PANEL] = middle_top_input_panel;
        lv_obj_set_align(middle_top_input_panel, LV_ALIGN_BOTTOM_MID);
        lv_obj_set_style_border_side(middle_top_input_panel, LV_BORDER_SIDE_FULL, 0);
        lv_obj_set_style_border_width(middle_top_input_panel, middle_top_input_panel_border_width, 0);
        lv_coord_t middle_top_input_panel_pad_left = label_left_margin - (lv_disp_get_hor_res(NULL) - middle_top_input_panel_width)/2-middle_top_input_panel_border_width;
        lv_obj_set_style_pad_left(middle_top_input_panel, middle_top_input_panel_pad_left, 0);

        lv_obj_t *middle_top_input_label = ui_create_default_label(middle_top_input_panel, "请输入要查找的单词", CHINESE_FONT_16_LIGHT);
//        lv_obj_t *middle_top_input_label = ui_create_default_label(middle_top_input_panel, "sh", CHINESE_FONT_16_LIGHT);
        lv_obj_set_align(middle_top_input_label, LV_ALIGN_LEFT_MID);
        // 字边距
        lv_obj_set_style_text_letter_space(middle_top_input_label, 2, 0);



        //中间底部单词列表 left_pad row_pad
        lv_coord_t middle_bottom_panel_row_pad = 11;
        lv_coord_t middle_bottom_panel_height = lv_disp_get_ver_res(NULL) - top_panel_height - middle_panel_row_pad - middle_top_panel_height;
        lv_obj_t *middle_bottom_panel = ui_create_column_panel(middle_panel, lv_disp_get_hor_res(NULL),middle_bottom_panel_height);
        children[UI_COMP_WORD_QUERY_KEYBOARD_BOTTOM_LIST_PANEL] = middle_bottom_panel;
        lv_obj_set_flex_align(middle_bottom_panel, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_START);
        lv_obj_set_style_pad_row(middle_bottom_panel, middle_bottom_panel_row_pad, 0);
        lv_obj_set_style_pad_left(middle_bottom_panel, label_left_margin, 0);
//        lv_obj_set_style_border_side(middle_bottom_panel, LV_BORDER_SIDE_TOP, 0);
//        lv_obj_set_style_border_width(middle_bottom_panel, 1, 0);



        lv_obj_add_event_cb(screen->screen,common_get_component_child_event_cb,COMMON_LV_EVENT_GET_COMP_CHILD,children);
        lv_obj_add_event_cb(screen->screen,common_del_component_child_event_cb,LV_EVENT_DELETE,children);

        lv_group_focus_obj(middle_top_input_panel);
    }

}

// 界面刷新
void ui_word_query_keyboard_fresh_screen(){
    LOG_INFO_WRAPPER("start");
    ui_fresh_search_word_list();
}

// 根据查找单词刷新单词列表
static void ui_fresh_search_word_list() {
    // 搜索单词列表
    if(word_query_module.data->word_list){
        free(word_query_module.data->word_list);
        word_query_module.data->word_list = NULL;
    }
    int pad_row = 0;
    // 获取输入框
    lv_obj_t *input_box_panel = common_ui_comp_get_child(word_query_module.screen->screen,UI_COMP_WORD_QUERY_KEYBOARD_INPUT_PANEL);
    lv_obj_t *middle_top_input_label = lv_obj_get_child(input_box_panel, 0);
    if(word_query_module.data->input_word_len > 0){
        word_query_module.data->word_list = word_query_search_word_list(word_query_module.data->input_word,&(word_query_module.data->word_list_size),5);
        pad_row = WORD_QUERY_RECENTLY_WORD_LIST_PAD_ROW;
        lv_label_set_text(middle_top_input_label, word_query_module.data->input_word);
    }else{
        word_query_module.data->word_list = word_query_get_recently_word_list(&(word_query_module.data->word_list_size),5);
        pad_row = WORD_QUERY_RECENTLY_WORD_LIST_PAD_ROW;
        lv_label_set_text(middle_top_input_label, "请输入要查找的单词");
    }
    // 刷新单词列表

    fresh_recently_word_list(word_query_module.data->word_list, word_query_module.data->word_list_size,input_box_panel,pad_row);



}

static void fresh_recently_word_list(word_query_word_entry_t *word_list,int size,lv_obj_t *prev_obj,int pad_row){
    lv_obj_t *middle_bottom_panel = common_ui_comp_get_child(word_query_module.screen->screen,UI_COMP_WORD_QUERY_KEYBOARD_BOTTOM_LIST_PANEL);
    word_query_event_data_t *prev_obj_event_data = lv_obj_get_user_data(prev_obj);
    lv_obj_t *top_return_panel = prev_obj_event_data->prev_obj;
    // 将top_panel和input_panel的next置空，防止list为null时
    prev_obj_event_data->next_obj = NULL;
    prev_obj_event_data = lv_obj_get_user_data(top_return_panel);
    prev_obj_event_data->next_obj = NULL;
    lv_obj_clean(middle_bottom_panel);
    // 在event_data中保存上一个和下一个word_list_child_panel
    lv_obj_t *prev_word_list_child_panel = NULL;
    // 填充子节点
    int first_node_index = 0;
    // 单词列表
    for (int i = 0; i < size; ++i) {
        lv_obj_t *word_list_child_panel = ui_create_without_flex_panel(middle_bottom_panel,LV_SIZE_CONTENT,20);
        lv_obj_set_style_pad_top(word_list_child_panel, -6, 0);
        lv_obj_set_style_border_side(word_list_child_panel, LV_BORDER_SIDE_BOTTOM,LV_STATE_FOCUSED);
        lv_obj_set_style_border_width(word_list_child_panel, 2, LV_STATE_FOCUSED);
        lv_obj_set_style_pad_left(word_list_child_panel,-2,LV_STATE_FOCUSED);
        lv_obj_t *word_list_child_label = ui_create_default_label(word_list_child_panel,(char *)word_list[i].word,CHINESE_FONT_16_LIGHT);
        // 如果是最近查询单词，则不加入焦点组和事件
        if(!strncmp(word_list[i].word,"最近查询单词:",WORD_QUERY_MAX_WORD_LEN)){
            first_node_index++;
            continue;
        }
        if(!strncmp(word_list[i].word,"当前词库，暂未收录该单词",WORD_QUERY_MAX_WORD_LEN)){
            continue;
        }
        if(!strncmp(word_list[i].word,"请重新输入，查找其他单词",WORD_QUERY_MAX_WORD_LEN)){
            continue;
        }
        word_query_event_data_t* event_data = malloc_set_zero(sizeof(word_query_event_data_t));
        event_data->index = i;
        event_data->prev_obj = prev_word_list_child_panel;
        // 让前一个 panel 的 next 指向当前 panel
        if (prev_word_list_child_panel){
            word_query_event_data_t *prev_event_data = lv_obj_get_user_data(prev_word_list_child_panel);
            if (prev_event_data) {
                prev_event_data->next_obj = word_list_child_panel;
            }
        }
        word_query_keyboard_add_event_common(word_list_child_panel, event_data);
        // 更新 prev 变量，为下一次循环做准备
        prev_word_list_child_panel = word_list_child_panel;

        // 如果是第一个节点，那么上一个节点就是prev_obj
        if(i == first_node_index){
            event_data->prev_obj = top_return_panel;
            // prev_obj的下一个节点和top_panel就是当前节点
            word_query_event_data_t *word_query_event_data = lv_obj_get_user_data(top_return_panel);
            word_query_event_data->next_obj = word_list_child_panel;
            word_query_event_data = lv_obj_get_user_data(prev_obj);
            word_query_event_data->next_obj = word_list_child_panel;
        }

        //最后一个的next 指向top_panel
        if(i == size - 1){

            event_data->next_obj = top_return_panel;
        }
    }
}

void ui_word_query_with_keyboard_show(){
    LOG_INFO_WRAPPER("start");
    if(word_query_module.screen->screen == NULL){
//        ui_word_query_data_init(word_query_module.data);
        ui_word_query_keyboard_screen_init(word_query_module.screen);
        ui_word_query_keyboard_fresh_screen();
    }
    word_query_module.screen->deinit_cb = ui_word_query_with_keyboard_deinit;
    LOG_INFO_WRAPPER("end");
}
void ui_word_query_with_keyboard_deinit(){
    LOG_INFO_WRAPPER("start");
    ui_screen_group_deinit_common(&(word_query_module.screen->screen),&(word_query_module.screen->group));
    LOG_INFO_WRAPPER("end");

//    void ui_desktop_show();
//    ui_desktop_show();
}
